﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BabyFood.Entities;
using BabyFood.Data;
using System.Data;

public partial class TimKiem : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["TuKhoa"] != null)
            {
                lbltukhoa.Text = Convert.ToString(Session["TuKhoa"]);
                Pager1.CurrentIndex = 1;
                HienThiTinTuc(Pager1.CurrentIndex);
            }
            else
            {
                lbltukhoa.Text = "Không tìm thấy bài viết";
            }
        }
        Session["Title"] = "Tìm Kiếm";
    }

    /// <summary>
    /// Hiển thị Loại tin
    /// </summary>
    private void HienThiTinTuc(int PageNo)
    {
        int TotalRows = 0;
        string WhereClause = "1=1 and TinNong=1 and (TomTat like N'%" + Convert.ToString(Session["TuKhoa"]) + "%' or NoiDung like N'%" + Convert.ToString(Session["TuKhoa"]) + "%' or TieuDe like N'% " + Common.StandardString(Convert.ToString(Session["TuKhoa"])) + "%')";
        DataSet dsTinTuc = DataRepository.Provider.ExecuteDataSet("TinTuc_GetPaged", WhereClause, "NgayCapNhat DESC", PageNo - 1, Pager1.PageSize);
        dlTinTuc.DataSource = dsTinTuc.Tables[0];
        dlTinTuc.DataBind();
        try
        {
            TotalRows = Convert.ToInt32(dsTinTuc.Tables[1].Rows[0][0]);
        }
        catch (Exception)
        {
        }
        Pager1.ItemCount = TotalRows;
        H_PageCount.Value = Pager1.PageCount.ToString();
        //if (TotalRows <= Pager1.PageSize)
        //    Pager1.Visible = false;
        //else
        //    Pager1.Visible = true;
    }

    protected void Pager1_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
        Pager1.CurrentIndex = currnetPageIndex;
        HienThiTinTuc(currnetPageIndex);
    }

    protected void cmdNext_Click(object sender, ImageClickEventArgs e)
    {
        if (Pager1.CurrentIndex < Convert.ToInt32(H_PageCount.Value))
        {
            Pager1.CurrentIndex = Pager1.CurrentIndex + 1;
            HienThiTinTuc(Pager1.CurrentIndex);
        }
    }

    protected void cmdPrevious_Click(object sender, ImageClickEventArgs e)
    {
        if (Pager1.CurrentIndex > 1)
        {
            Pager1.CurrentIndex = Pager1.CurrentIndex - 1;
            HienThiTinTuc(Pager1.CurrentIndex);
        }
    }

    public string GetTieuDe(object Char)
    {
        string s = Char.ToString();
        return Common.ConvertToUnSign(s);
    }
}